package net.sourceforge.m2settings.gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import net.sourceforge.m2settings.model.ExtendableTableModel;

import org.apache.log4j.Logger;

public class CreateElementFrame extends JFrame implements ActionListener {

	private static final long serialVersionUID = 1L;

	private JPanel jContentPane = null;

	private ExtendableTableModel model = null;

	private JPanel jPanelModel = null;

	private JPanel jPanelButtons = null;

	private JButton jButtonOK = null;

	private JButton jButtonCancel = null;

	private JLabel[] jLabels = null;

	private JTextField[] jTextFields = null;
	private Logger log = Logger.getLogger(CreateElementFrame.class); // @jve:decl-index=0:

	/**
	 * This is the default constructor
	 */
	public CreateElementFrame(ExtendableTableModel model) {
		super();
		this.model = model;
		log.debug("Initializes the Frame with model " + model);
		log.debug("There are " + model.getColumnCount()
				+ " fields in this model");
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(300, 200);
		this.setContentPane(getJContentPane());
		this.setTitle("Create a new "+model.getElementName());
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jLabels = new JLabel[model.getColumnCount()];
			jTextFields = new JTextField[model.getColumnCount()];
			for (int i = 0; i < model.getColumnCount(); i++) {
				jLabels[i] = new JLabel();
				jLabels[i].setText(model.getColumnName(i));
				jTextFields[i] = new JTextField();
				jTextFields[i].setText("");
			}
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJPanelModel(), BorderLayout.CENTER);
			jContentPane.add(getJPanelButtons(), BorderLayout.SOUTH);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jPanelModel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanelModel() {
		if (jPanelModel == null) {
			GridLayout gridLayout = new GridLayout();
			gridLayout.setRows(model.getColumnCount());
			gridLayout.setColumns(2);
			jPanelModel = new JPanel();
			jPanelModel.setLayout(gridLayout);
			for (int i = 0; i < model.getColumnCount(); i++) {
				jPanelModel.add(jLabels[i], null);
				jPanelModel.add(jTextFields[i], null);
			}
		}
		return jPanelModel;
	}

	/**
	 * This method initializes jPanelButtons
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanelButtons() {
		if (jPanelButtons == null) {
			jPanelButtons = new JPanel();
			jPanelButtons.setLayout(new FlowLayout());
			jPanelButtons.add(getJButtonOK(), null);
			jPanelButtons.add(getJButtonCancel(), null);
		}
		return jPanelButtons;
	}

	/**
	 * This method initializes jButtonOK
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButtonOK() {
		if (jButtonOK == null) {
			jButtonOK = new JButton();
			jButtonOK.setText("OK");
			jButtonOK.addActionListener(this) ;
		}

		return jButtonOK;
	}

	/**
	 * This method initializes jButtonCancel
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButtonCancel() {
		if (jButtonCancel == null) {
			jButtonCancel = new JButton();
			jButtonCancel.setText("Cancel");
			jButtonCancel
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {
							// TODO Confirm dialog
							dispose();
						}
					});
		}
		return jButtonCancel;
	}
	
	
	public void actionPerformed(java.awt.event.ActionEvent e) {
		log.debug("The model contains " + model.getClass());// MirrorTableModel
		model.addRow();
		int rowIndex = model.getRowCount()-1;
		log.debug("added line #"+rowIndex);
		for (int columnIndex = 0; columnIndex < model
				.getColumnCount(); columnIndex++) {
			model.setValueAt(jTextFields[columnIndex].getText(), rowIndex,
					columnIndex);
		}
		dispose();
	}
}
